
package com.netease.xone.fragment;

import protocol.XoneCallBack;
import protocol.XoneService;
import Utils.StackTraceUtil;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.netease.log.NTLog;
import com.netease.pkgRelated.XoneConsts.BUNDLE_KEY;
import com.netease.pkgRelated.XoneConsts.MENU_ID;
import com.netease.share.ShareType;
import com.netease.xone.R;
import com.netease.xone.widget.CustomDialog.OnCustomDlgCancelListener;
import com.netease.xone.widget.LoadingImageView;

public class FragmentShare extends FragmentBase {

    public static final String TAG = FragmentShare.class.getSimpleName();
    
    // 事物id
    private int mTid;
    // 分享类型
    private ShareType mShareType;
    
    // 输入内容
    private EditText mContent;
    // 图片
    private LoadingImageView mImageView;
    // 剩余数字提示
    private LinearLayout mTip;
    // 剩余字数
    private TextView mRemainCount;
    
    public static FragmentShare newInstance(int type) {
        FragmentShare f = new FragmentShare();
        Bundle b = new Bundle();
        b.putInt(BUNDLE_KEY.WB_BIND_TYPE, type);
        f.setArguments(b);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	Bundle bundle = getArguments();
        int type = 0;
        if (bundle != null && bundle.containsKey(BUNDLE_KEY.WB_LOGIN_TYPE)
                && bundle.containsKey(BUNDLE_KEY.WB_BIND_TYPE)) {
            type = bundle.getInt(BUNDLE_KEY.WB_BIND_TYPE);
        }
        
        if (type > 0) {
            mShareType = ShareType.valueOf(type);
        }
        
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        View view = inflater.inflate(R.layout.fragment_share, container, false);
        findViews(view);
        return view;
    }

    private void findViews(View view) {
        mContent = (EditText)view.findViewById(R.id.content);
        mImageView = (LoadingImageView)view.findViewById(R.id.picture);
        mTip = (LinearLayout)view.findViewById(R.id.tip);
        mRemainCount = (TextView)view.findViewById(R.id.count);
        
        mImageView.setVisibility(View.GONE);
        mTip.setVisibility(View.GONE);
        
        mContent.addTextChangedListener(mTextWatcher);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

        com.actionbarsherlock.view.MenuItem item = menu.add(Menu.NONE, MENU_ID.menu_feedback_send,
                0, getString(R.string.done));
        item.setIcon(R.drawable.icon_action_done_selector);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        switch (item.getItemId()) {
            case MENU_ID.menu_feedback_send:
                send();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    private void send() {
        
    }
    
    private static final int MAX_SINA = 140;
    private static final int MAX_NETEASE = 163;
    private static final int MAX_QQMBLOG = 140;
    private static final int MAX_TENCENT = 2000;
    private static final int MAX_RENREN = 240;
    
    private static final int TIP_MAX = 20;
    
    private void changeTitle(int len){
    	String str = "" + len;
    	int visibility = View.GONE;
    	
    	if (len < 0) {
    		visibility = View.VISIBLE;
		} else if (len <= TIP_MAX) {
			visibility = View.VISIBLE;
		}
    	mTip.setVisibility(visibility);
    	
    	mRemainCount.setText(str);
    }
    
    private TextWatcher mTextWatcher = new TextWatcher() {

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
		}

		@Override
		public void afterTextChanged(Editable s) {
			int len = s.length();
			int maxLen = 0;
			switch (mShareType) {
			case Sina:
				maxLen = MAX_SINA;
				break;
			case Netease:
				maxLen = MAX_NETEASE;
				break;
			case Qqmblog:
				maxLen = MAX_QQMBLOG;
				break;
			case Tencent:
				maxLen = MAX_TENCENT;
				break;
			case Renren:
				maxLen = MAX_RENREN;
				break;
			}
			
			changeTitle(maxLen - len);
		}
    	
    };
    
    private OnCustomDlgCancelListener mOnCustomDlgCancelListener = new OnCustomDlgCancelListener() {

        @Override
        public void onCancel() {
            XoneService.getInstance().cancelTransaction(mTid);
        }
    };

    @Override
    public void onDestroy() {
        XoneService.getInstance().removeListener(mXoneCallBack);
        mXoneCallBack = null;
        super.onDestroy();
        
        mContent.removeTextChangedListener(mTextWatcher);
        mContent = null;
        mTextWatcher = null;
    }

    XoneCallBack mXoneCallBack = new XoneCallBack() {
    	
    };
}
